package school.DAO.impl;

import java.lang.reflect.ParameterizedType;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import school.DAO.ICommonDAO;
@Component
public class CommonDAO<T> implements ICommonDAO<T> {
	@PersistenceContext
	private EntityManager em;
	
	public void delete(T t) {
		em.remove(t);
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly=true)
	public T findById(int id) {
		Class<T> entityClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; 
		T t=(T)em.find(entityClass, id);
		return t;
	}
	@Transactional
	public void insert(T t) {
		em.persist(t);
	}
	@Transactional
	public void update(Object t) {
		em.merge(t);
	}

}
